package com.example.personalblog.controller;

import com.example.personalblog.service.ArticleService;
import com.example.personalblog.vo.DisplayArticle;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class IndexController {

    @Autowired
    ArticleService articleService;

    @RequestMapping({"/"})
    public String index(Model model){
        PageInfo<DisplayArticle> hottest = articleService.findHottest();
        PageInfo<DisplayArticle> pageInfo = articleService.findLatest(1);

        model.addAttribute("hottest",hottest);
        model.addAttribute("pageInfo", pageInfo);
        return "index";
    }

    @RequestMapping({"/{pageNum}"})
    public String index(Model model, @PathVariable int pageNum){
        PageInfo<DisplayArticle> hottest = articleService.findHottest();
        PageInfo<DisplayArticle> pageInfo = articleService.findLatest(pageNum);

        model.addAttribute("hottest",hottest);
        model.addAttribute("pageInfo", pageInfo);
        return "index";
    }

    @GetMapping("/about")
    public String about(){
        return "about";
    }
}
